package com.shine.minepressure.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;

import com.shine.minepressure.BR;
import com.shine.minepressure.R;
import com.shine.minepressure.app.Constants;
import com.shine.minepressure.base.BaseActivity;
import com.shine.minepressure.bean.LoginBean;
import com.shine.minepressure.databinding.ActivitySetIpBinding;
import com.shine.minepressure.http.ApiException;
import com.shine.minepressure.http.ECObserver;
import com.shine.minepressure.http.RetrofitClient;
import com.shine.minepressure.http.RxUtils;
import com.shine.minepressure.http.Service;
import com.shine.minepressure.ui.base.BaseDameViewModel;
import com.shine.minepressure.utils.SPUtils;

public class SetIpActivity extends BaseActivity<ActivitySetIpBinding, BaseDameViewModel> {
    private String ip, port;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_set_ip;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public BaseDameViewModel initViewModel() {
        return new BaseDameViewModel(this);
    }

    @Override
    public int appTheme() {
        return Constants.THEME_FULL;
    }

    @Override
    public void initData() {
        super.initData();
        setStatusBar();

        initTitle(this, "设置服务地址");
        //https://172.16.18.237:8075/
        //https://172.16.18.237:8074/WebService_KJ508.asmx/KJ508_Get_LoginState?UserName=admin&PassWord=kj508

        //https://ky.dev.3shine.com/WebService_KJ508.asmx/KJ508_Get_LoginState?UserName=admin&PassWord=kj508

        if (!TextUtils.isEmpty(SPUtils.getInstance().getString(Constants.SP_IP, ""))) {
            binding.ipEdit.setText(SPUtils.getInstance().getString(Constants.SP_IP, ""));
            binding.portEdit.setText(SPUtils.getInstance().getString(Constants.SP_PORT, ""));
            binding.nameEdit.setText(SPUtils.getInstance().getString(Constants.SP_LOGIN_NAME, ""));
            binding.passwordEdit.setText(SPUtils.getInstance().getString(Constants.SP_LOGIN_PASS, ""));
        }

//        binding.ipEdit.setText("http://172.16.18.237");
//        binding.portEdit.setText("8074");
//        binding.nameEdit.setText("admin");
//        binding.passwordEdit.setText("jh508");

        binding.testIp.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.testIp:
                if (binding.ipEdit.getText().toString().isEmpty()) {
                    ToastMessage("请输入IP");
                    return;
                }
//                if (binding.portEdit.getText().toString().isEmpty()) {
//                    ToastMessage("请输入新端口号");
//                    return;
//                }
                if (binding.nameEdit.getText().toString().isEmpty()) {
                    ToastMessage("请输入用户名");
                    return;
                }
                if (binding.passwordEdit.getText().toString().isEmpty()) {
                    ToastMessage("请输入密码");
                    return;
                }

                if (binding.ipEdit.getText().toString().startsWith("http://"))
                    ip = binding.ipEdit.getText().toString();
                else if (binding.ipEdit.getText().toString().startsWith("https://"))
                    ip = binding.ipEdit.getText().toString();
                else
                    ip = "https://" + binding.ipEdit.getText().toString();

                port = binding.portEdit.getText().toString();

                String url = ip + ":" + port + Constants.ADDRESS;
                System.out.println(url);
                SPUtils.getInstance().put(Constants.SP_URl, url);

                loginByPassword(url);
                break;
            default:
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                default:
                    break;
            }
        }
    }

    private void loginByPassword(String url) {
        showLoading("");
        RetrofitClient.getInstance(url).create(Service.class)
                .LoginAuthentication(binding.nameEdit.getText().toString().trim(), binding.passwordEdit.getText().toString().trim())
                .compose(RxUtils.bindToLifecycle(this))
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .subscribe(new ECObserver<LoginBean>() {
                    @Override
                    protected void _onNext(LoginBean loginBean) {
                        dismissLoading();
//                        LoginBean loginBean = new Gson().fromJson(str, LoginBean.class);

                        SPUtils.getInstance().put(Constants.SP_IP, ip);
                        SPUtils.getInstance().put(Constants.SP_PORT, port);
                        SPUtils.getInstance().put(Constants.SP_LOGIN_NAME, binding.nameEdit.getText().toString().trim());
                        SPUtils.getInstance().put(Constants.SP_LOGIN_PASS, binding.passwordEdit.getText().toString().trim());

                        SPUtils.getInstance().put(Constants.SP_URl,
                                ip
                                + ":"
                                + port
                                + Constants.ADDRESS);

                        finishActivityForResult();
                    }

                    @Override
                    protected void _onError(ApiException ex) {
                        ToastMessage(ex.message);
                        dismissLoading();
                    }
                });
    }
}
